package com.senjoeson.teamfriends.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatAutoCompleteTextView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.senjoeson.teamfriends.R;
import com.senjoeson.teamfriends.bean.User;
import com.senjoeson.teamfriends.utils.FormatUtils;
import com.senjoeson.teamfriends.utils.MyToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.email)
    AppCompatAutoCompleteTextView mEmail;
    @BindView(R.id.userpwd)
    AppCompatAutoCompleteTextView mUserpwd;
    @BindView(R.id.login)
    Button mLogin;
    @BindView(R.id.toregister)
    Button mToregister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        mToregister.setOnClickListener(this);
        mLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toregister:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                onBackPressed();
                break;
            case R.id.login:
                login();
                break;
        }


    }

    /**
     * 登录逻辑
     */
    private void login() {
        final String emails = mEmail.getText().toString().trim();
        final String mUserpass = mUserpwd.getText().toString().trim();
        if (TextUtils.isEmpty(emails) || TextUtils.isEmpty(mUserpass)) {
            Toast.makeText(this, "邮箱或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        } else if (!FormatUtils.checkEmail(emails)) {
            MyToast.showToast("邮箱格式不正确");
            return;
        }

        BmobUser.loginByAccount(emails, mUserpass, new LogInListener<User>() {

            @Override
            public void done(User user, BmobException e) {
                if (user != null) {
                    MyToast.showToast("登陆成功");
                    //   SPUtils.putString("userid", user.getObjectId());
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else {
                    MyToast.showToast("错误信息:" + e.toString());
                }
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
